package unitTests_Comunication;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import Email.auto.checker.Comunication;
import Email.auto.checker.ConstAccount;
import Email.auto.checker.ConstComunication;

public class TestsComunication {

	Comunication comunication;
	ConstComunication constCom;
	ConstAccount constAcc;

	@Before
	public void setUp(){
		comunication = new Comunication();
	}
	

	@Test
	public void setServerAndDisconnect() {
		assertEquals(constCom.COMUNICATION_OK, comunication.setServer(constCom.HOTMAIL_SERVER));
		assertEquals(constCom.COMUNICATION_OK, comunication.endComunication(constCom.HOTMAIL_SERVER));
		
		assertEquals(constCom.COMUNICATION_OK, comunication.setServer(constCom.GMAIL_SERVER));
		assertEquals(constCom.COMUNICATION_OK, comunication.endComunication(constCom.GMAIL_SERVER));
		
		assertEquals(constCom.COMUNICATION_OK, comunication.setServer(constCom.ISEC_SERVER));
		assertEquals(constCom.COMUNICATION_OK, comunication.endComunication(constCom.ISEC_SERVER));
		
		assertEquals(constCom.COMUNICATION_ERROR, comunication.setServer("RandomText"));
		assertEquals(constCom.COMUNICATION_ERROR, comunication.setServer(""));
	
		
	
	}
	
	@Test
	public void loginServerAndDisconect_GMAIL(){
		//Valid email
		String correct_request = ConstComunication.LOGIN_IMAP +" "+ "gpstestes2000@gmail.com" + " " + "palavrapasse";
		
		//Try make login without server seted
		assertTrue(comunication.login(correct_request, null) == false);
	
		//Set servers and make Logins with correct data
		assertEquals(constCom.COMUNICATION_OK, comunication.setServer(constCom.GMAIL_SERVER));
		assertTrue(comunication.login(correct_request, null) == true);
		assertEquals(constCom.COMUNICATION_OK, comunication.endComunication(constCom.GMAIL_SERVER));
		
		//Set servers and make Logins with incorrect data
		String incorrect_request = ConstComunication.LOGIN_IMAP +" "+ "mistercimba2000@gmail.com" + " " + "brunoaleixo2000";
		
		assertEquals(constCom.COMUNICATION_ERROR, comunication.setServer(constCom.GMAIL_SERVER));
		assertFalse(comunication.login(incorrect_request, null) == false);
		assertEquals(constCom.COMUNICATION_ERROR, comunication.endComunication(constCom.GMAIL_SERVER));
	}
	
	
	@Test
	public void loginServerAndDisconect_ISEC(){
		//Valid email
		String correct_request = ConstComunication.LOGIN_IMAP +" "+ "a21200791@isec.pt" + " " + "palavrapasse";
		
		//Try make login without server seted
		assertTrue(comunication.login(correct_request, null) == false);
	
		//Set servers and make Logins with correct data
		assertEquals(constCom.COMUNICATION_OK, comunication.setServer(constCom.ISEC_SERVER));
		assertTrue(comunication.login(correct_request, null) == true);
		assertEquals(constCom.COMUNICATION_OK, comunication.endComunication(constCom.ISEC_SERVER));
		
		//Set servers and make Logins with incorrect data
		String incorrect_request = ConstComunication.LOGIN_IMAP +" "+ "mistercimba@isec.pt" + " " + "brunoaleixo2000";
		
		assertEquals(constCom.COMUNICATION_ERROR, comunication.setServer(constCom.ISEC_SERVER));
		assertTrue(comunication.login(incorrect_request, null) == false);
		assertEquals(constCom.COMUNICATION_ERROR, comunication.endComunication(constCom.ISEC_SERVER));
	}
	
	@Test
	public void loginServerAndDisconect_HOTMAIL(){
		//Valid email
		String user_correct_request = ConstComunication.LOGIN_HOTMAIL +" "+ "email@hotmail.com";
		String pass_correct_request = ConstComunication.PASS_HOTMAIL + " " + "password";	
		//Try make login without server seted
		assertTrue(comunication.login(user_correct_request, pass_correct_request) == false);
	
		//Set servers and make Logins with correct data
		assertEquals(constCom.COMUNICATION_OK, comunication.setServer(constCom.HOTMAIL_SERVER));
		assertTrue(comunication.login(user_correct_request, pass_correct_request) == true);
		assertEquals(constCom.COMUNICATION_OK, comunication.endComunication(constCom.HOTMAIL_SERVER));
		
		//Set servers and make Logins with incorrect data
		String incorrect_request = ConstComunication.LOGIN_IMAP +" "+ "mistercimba@hotmail.com" + " " + "brunoaleixo2000";
		
		assertEquals(constCom.COMUNICATION_ERROR, comunication.setServer(constCom.HOTMAIL_SERVER));
		assertTrue(comunication.login(user_correct_request, pass_correct_request) == false);
		assertEquals(constCom.COMUNICATION_ERROR, comunication.endComunication(constCom.HOTMAIL_SERVER));
	}
	
	@Test
	public void getUnreadedMessages_GMAIL(){
		//Valid request
		String correct_request = ConstComunication.LOGIN_IMAP +" "+ "gpstestes2000@gmail.com" + " " + "palavrapasse";
		String correct_message_request = ConstComunication.UNSEEN_MESSAGES_IMAP;
		
		//Login correctly in the server
		assertEquals(constCom.COMUNICATION_OK, comunication.setServer(constCom.GMAIL_SERVER));
		assertTrue(comunication.login(correct_request, null) == true);
		assertEquals(constCom.COMUNICATION_OK, comunication.endComunication(constCom.GMAIL_SERVER));
		
		//After logging in get messages
		assertEquals(0,comunication.getUnreadedMessages(constAcc.GMAIL_DOMAIN));
		assertTrue(comunication.getUnreadedMessages(constAcc.GMAIL_DOMAIN) >= 0);
		
		//get messages fail
		assertEquals(0,comunication.getUnreadedMessages(constAcc.GMAIL_DOMAIN));
		assertTrue(comunication.getUnreadedMessages(constAcc.GMAIL_DOMAIN) < 0);		
		
	}
	
	@Test
	public void getUnreadedMessages_ISEC(){
		//Valid request
		String correct_request = ConstComunication.LOGIN_IMAP +" "+ "a21200791@isec.pt" + " " + "brunoaleixo2000";
		String correct_message_request = ConstComunication.UNSEEN_MESSAGES_IMAP;
		
		//Login correctly in the server
		assertEquals(constCom.COMUNICATION_OK, comunication.setServer(constCom.ISEC_SERVER));
		assertTrue(comunication.login(correct_request, null) == true);
		assertEquals(constCom.COMUNICATION_OK, comunication.endComunication(constCom.ISEC_SERVER));
		
		//After logging in get messages
		assertEquals(0,comunication.getUnreadedMessages(constAcc.ISEC_DOMAIN));
		assertTrue(comunication.getUnreadedMessages(constAcc.ISEC_DOMAIN) >= 0);
		
		//get messages fail
		assertEquals(0,comunication.getUnreadedMessages(constAcc.ISEC_DOMAIN));
		assertTrue(comunication.getUnreadedMessages(constAcc.ISEC_DOMAIN) < 0);	
	}
	
	@Test
	public void getUnreadedMessages_HOTMAIL(){
		//Valid request
		String correct_user_request = ConstComunication.LOGIN_HOTMAIL +" "+ "mistercimba@hotmail.com";
		String correct_pass_request = ConstComunication.PASS_HOTMAIL + " " + "brunoaleixo2000";
		String correct_message_request = ConstComunication.UNSEEN_MESSAGES_POP;
		
		//Login correctly in the server
		assertEquals(constCom.COMUNICATION_OK, comunication.setServer(constCom.HOTMAIL_SERVER));
		assertTrue(comunication.login(correct_user_request, correct_pass_request) == true);
		assertEquals(constCom.COMUNICATION_OK, comunication.endComunication(constCom.HOTMAIL_SERVER));
		
		//After logging in get messages
		assertEquals(0,comunication.getUnreadedMessages(constAcc.HOTMAIL_DOMAIN));
		assertTrue(comunication.getUnreadedMessages(constAcc.HOTMAIL_DOMAIN) >= 0);
		
		//get messages fail
		assertEquals(0,comunication.getUnreadedMessages(constAcc.HOTMAIL_DOMAIN));
		assertTrue(comunication.getUnreadedMessages(constAcc.HOTMAIL_DOMAIN) < 0);	
	}
}
